﻿using Demo.Net.EFCore.Entity;
using Demo.Net.Infrastructure;
using Demo.Net.Service.Service;
using Demo.Net.SqlSugar;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System.Security.Cryptography;

namespace Demo.Net.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class TestController(SqlSugarService _sqlSugarService, DapperService _dapperService, EFCoreService _efcoreService) : ControllerBase
    {

        //private readonly SqlSugarService _sqlSugarService;
        //private readonly DapperService _dapperService;
        //private readonly EFCoreService _efcoreService;


        //public TestController(IServiceProvider provider)
        //{
        //    _sqlSugarService = provider.GetService<SqlSugarService>();
        //    _dapperService = provider.GetService<DapperService>();
        //    _efcoreService = provider.GetService<EFCoreService>();
        //}



       


        /// <summary>
        /// sqlsugar调用测试
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> SqlSugarTest()
        {
            var result = await _sqlSugarService.GetTest();
            return Ok(result);

        }

        /// <summary>
        /// 添加用户测试
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="userPwd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> AddUser(string userName, string userPwd)
        {
            var salt = RandomHelper.NextString(8);
            var user = new Tusr_User
            {
                UserName = userName,
                UserPwd = SecurityHelper.MD5WithSalt(userPwd, salt),   // 登入密码
                PwdSalt = salt
            };
            var result = await _sqlSugarService.AddUser(user);
            return Ok(result);

        }


        /// <summary>
        /// dapper调用测试
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Test()
        {
            return Ok("123");

        }


        /// <summary>
        /// dapper调用测试
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> DapperTest()
        {
            var result = await _dapperService.GetTest();
            return Ok(result);

        }

        /// <summary>
        /// efcode测试
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> EFCoreTest([FromBody] tusr_user user)
        {
            await _efcoreService.Update(user); //await _efcreService.GetTest();
            return Ok();

        }


    }
}
